package com.qf;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>title: com.qf</p>
 * author zhuximing
 * description:
 */
public class TestFreemarker {
    public static void main(String[] args) throws  Exception {

        //1.构建Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板文件目录(Directory)
        configuration.setDirectoryForTemplateLoading(new File("D:\\JAVAEE-2009班\\7.微服务项目实战\\代码\\portal-parent\\study-freemarker\\src\\main\\resources"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.获取模板对象
        Template template = configuration.getTemplate("templates/demo.ftl");
        //5.创建模型数据

        Student student = new Student("jack",12);
        Student student1 = new Student("rose",18);
        Student student2 = new Student("james",19);
        List stus = new ArrayList(){{
            add(student);
            add(student1);
            add(student2);
        }};

        Map data = new HashMap(){{
            put("username","张三丰");
            put("age",19);
            put("stus",stus);
            put("num",1000000);
        }};
        //6.创建输出流(Writer)对象
        Writer out = new FileWriter("e:\\demo.html");
        //7.输出
        template.process(data,out);
        //8.关闭
        out.close();
    }
}